package dominion.player;

import dominion.Dominion;
import dominion.Player;
import dominion.Tresure;
import dominion.kingdom.Garden;
import dominion.kingdom.Smithy;
import dominion.kingdom.Village;
import dominion.kingdom.Workshop;

public class WorkshopGardenPlayer extends AbstractPlayer {

	int garden;
	int village;
	int smithy;
	int workshop;

	public WorkshopGardenPlayer(Dominion dominion) {
		this("WorkshopGarden", dominion);
	}

	public WorkshopGardenPlayer(String name, Dominion dominion) {
		super(name, dominion);
	}

	@Override
	public int buy(Dominion dominion, int value) {
		if (5 < value && dominion.buy(this, Tresure.GOLD)) {
			value -= 6;
		} else if (smithy < 3 && 3 < value && dominion.buy(this, new Smithy())) {
			smithy++;
			value -= 4;
		} else if (village < 3 && 2 < value
				&& dominion.buy(this, new Village())) {
			village++;
			value -= 3;
		} else if (2 < value) {
			if (((3 < (workshop - garden)) && dominion.buy(this,
					new GardenWorkshop()))
					|| (1 < (workshop - village) && dominion.buy(this,
							new VillageWorkshop()))) {
				workshop++;
				value -= 3;
			}
		}
		return value;
	}

	class GardenWorkshop extends Workshop {
		@Override
		public void execute(Dominion dominion) {
			Player player = dominion.current();
			if (dominion.buy(player, new Garden(player))) {
				garden++;
			} else if (dominion.buy(player, new Smithy())) {
				smithy++;
			} else if (dominion.buy(player, new VillageWorkshop())) {
				workshop++;
			}
		}
	}

	class VillageWorkshop extends Workshop {
		@Override
		public void execute(Dominion dominion) {
			Player player = dominion.current();
			if (dominion.buy(player, new Village())) {
				village++;
			} else if (dominion.buy(player, new Smithy())) {
				smithy++;
			} else if (dominion.buy(player, new GardenWorkshop())) {
				workshop++;
			}
		}
	}
}
